import Taro from '@tarojs/taro-rn';
import { ServerHost } from "./common";
import React from 'react';

// import wx from '_weixin-js-sdk'
// let wx = require('weixin-js-sdk');
export const getJsApiParam = () => {
  let currentUrl = window.location.href;
  let apiUrl = `${ServerHost}/api/Account/getWxJsApiParams?curUrl=${currentUrl}`;
  Taro.showLoading({ title: '加载中...' });
  fetch(apiUrl, {
    method: 'GET',
    mode: "cors",
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    credentials: 'same-origin'
  }).then(response => {
    Taro.hideLoading();
    return response.json();
  }).then(response => {
    let appId = response.appId;
    let timestamp = response.timestamp;
    let nonceStr = response.nonceStr;
    let signature = response.signature;
    wx.config({
      debug: false,
      appId,
      timestamp,
      nonceStr,
      signature,
      jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
    });
  }).catch(error => {
    console.log(error);
  });
};
export const WxShare = async (url, img, name, desc) => {
  wx.ready(function () {
    wx.onMenuShareAppMessage({
      title: name,
      desc,
      link: url,
      imgUrl: img
    });
    wx.onMenuShareTimeline({
      title: name,
      link: url1,
      imgUrl: img
    });
  });
  wx.error(function (res) {
    console.log('微信接口校验失败');
  });
};